package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.entity.User;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private IUserService userService;
    @Override
    //来查询用户当前关注还是取关的功能
    public Result follow(Long followUserId, boolean isFollow) {
        Long userId = UserHolder.getUser().getId();
        String key="follow:"+userId;
        if(isFollow){
            //进行关注
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(followUserId);
            //先保存到数据库中
            boolean isSuccess = save(follow);
            //如果存入成功，将当前用户的关注对象放进redis中的set集合中
            if(isSuccess){
                stringRedisTemplate.opsForSet().add(key,followUserId.toString());
            }
        }else {
            //取关即分别删除mysql和redis中的数据
            remove(new QueryWrapper<Follow>().eq("user_id",userId).eq("follow_user_id",followUserId));
            //删除redis中
            stringRedisTemplate.opsForSet().remove(key,followUserId.toString());
        }
        return Result.ok();
    }

    @Override
    //查询用户是否被关注
    public Result isFollow(Long followUserId) {
        Long userId = UserHolder.getUser().getId();
        String key="follow:"+userId;
        Integer count = query().eq("user_id", userId).eq("follow_user_id", followUserId).count();
        //即mysql中有用户数据则代表关注过
        return Result.ok(count>0);
    }

    @Override
    //查询共同关注的用户
    public Result commons(Long followUserId) {
        //获取当前用户id
        Long userId = UserHolder.getUser().getId();
        String key="follow:"+userId;
        //获取关注用户的键
        String followKey = "follow:"+followUserId;
        //从redis中获取交集
        Set<String> set = stringRedisTemplate.opsForSet().intersect(key, followKey);
        if(set==null||set.isEmpty()){
            return Result.ok(Collections.emptyList());
        }
        //解析set中的用户id
        List<Long> ids = set.stream().map(Long::valueOf).collect(Collectors.toList());
        //从id集合中查询出对应的id用户
        List<UserDTO> userDTOList = userService.listByIds(ids).stream().map(user -> BeanUtil.copyProperties(user, UserDTO.class)).collect(Collectors.toList());
        return Result.ok(userDTOList);
    }
}
